# !/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
from config.db import brokersDb  as brokersConf

class valida(object):
    def __init__(self, structuredData):
        """
        数据初始化
        """
        self.structuredData = structuredData
        self.resutl = {}

    def main(self):
        """
        数据验证入口
        :return:
        """
        phone  = self.structuredData.get("phone")
        city  = self.structuredData.get("city")

        #TODO 手机号验证
        self.phoneStatus(phone=phone)

        #TODO 经纪人验证
        self.tourists( phone, city=city )


        return self.resutl


    def tourists(self, phone, city):
        """
        判断是否为客源，
        true：客源
        false：经纪人
        :return:
        """
        dbName = brokersConf.get("database") % (city)
        conn = pymysql.connect(
            host = brokersConf.get("host"),
            port = brokersConf.get("port"),
            user = brokersConf.get("user"),
            passwd = brokersConf.get("password"),
            db = dbName,
            charset = "utf8"
        )

        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "select id, name, phone from broker where phone = '%s'" % (phone)
        cursor.execute(sql)
        result = cursor.fetchone()

        cursor.close()
        conn.close()

        # 经纪人
        if result:
            self.resutl.setdefault("tourist_class", 2)

        # 非经纪人
        else:
            self.resutl.setdefault("tourist_class", 1)



    def phoneStatus(self, phone):
        """
        手机号码状态
        4:空号、
        1 有效
        :return:
        """
        tourists = True
        if tourists == True:
            self.resutl.setdefault("status", 0)

        else:
            self.resutl.setdefault("status", 4)
